<template>
  <div class="app-container pagination">
    <el-card>
      <div class="header">
        <div>
          <el-date-picker
            v-model="search['date']"
            class="margin-right-20"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            @change="()=>pagination['load']()"/>
          <el-select v-model="search['status']" placeholder="提现状态" class="select" clearable @change="()=>pagination['load']()">
            <el-option value="1" label="申请中"/>
            <el-option value="2" label="已同意"/>
            <el-option value="3" label="已拒绝"/>
          </el-select>
          <el-input v-model="search['search']" placeholder="请输入用户名或订单号" class="search-input"/>
          <el-button type="primary" icon="el-icon-search" @click="()=>pagination['load']()">搜索</el-button>
        </div>
        <div>
          <el-button :loading="downloadLoading" :disabled="downloadLoading" type="danger" icon="el-icon-printer" @click="handleDownload">批量导出</el-button>
        </div>
      </div>
      <ws-pagination v-model="pagination">
        <el-table-column prop="id" label="ID" min-width="65" header-align="center" align="center" sortable />
        <el-table-column prop="driverName" label="提现人名称" min-width="120" header-align="center" align="center" />
        <el-table-column prop="money" label="提现金额" min-width="120" header-align="center" align="center" />
        <el-table-column prop="transferSn" label="订单号" min-width="150" header-align="center" align="center" />
        <el-table-column label="支付订单号" min-width="180" header-align="center" align="center" >
          <template slot-scope="scope">
            {{ !scope.row['tradeNo']?'--':scope.row['tradeNo'] }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="申请时间" min-width="160" header-align="center" align="center" sortable/>
        <!--<el-table-column label="处理人" min-width="90" header-align="center" align="center" >
          <template slot-scope="scope">
            {{ !scope.row['handle_name']?'&#45;&#45;':scope.row['handle_name'] }}
          </template>
        </el-table-column>-->
        <el-table-column label="处理状态" min-width="110" header-align="center" align="center" fixed="right">
          <template slot-scope="scope">
            {{ scope.row['status']==1?'申请中':scope.row.status==2?'已同意':'已拒绝' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="220" header-align="center" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button v-if="scope.row['status']==1" type="success" size="small" icon="el-icon-check" circle
                       class="bg-green" @click="setStatus(scope.row, 2)"/>
            <el-button v-if="scope.row['status']==1" type="danger" size="small" icon="el-icon-close" circle
                       @click="setStatus(scope.row, 3)"/>
            <el-button type="info" size="small" icon="el-icon-delete" circle @click="deleteRow(scope['row'])"/>
          </template>
        </el-table-column>
      </ws-pagination>
    </el-card>
  </div>
</template>

<script>
  import Config from '../../config';
  export default {
    name: 'FinancialApplyList',
    components: {},
    data() {
      return {
        // 查询条件
        search: {
          search: '',
          // 时间
          date: [],
          // 时间起点
          timeFrom: '',
          // 时间终点
          timeTo: '',
          // 提现状态
          status: '',
          // 用户状态
          userStatus: ''
        },
        pagination: {},
        downloadLoading: false
      }
    },
    watch: {},
    created() {
      // 初始化分页器
      this.initPagination();
    },
    methods: {
      // 初始化分页器
      initPagination() {
        // 创建分页器
        this.pagination = new this.$pagination(async (...args) => {
          // 加载分页列表
          this.search.timeFrom = null;
          this.search.timeTo = null;
          this.search.date && this.search.date.length !== 0 && (this.search.timeFrom = this.search.date[0] + ' 00:00:00', this.search.timeTo = this.search.date[1] + ' 23:59:59')
          const result = await this.$service.applicationGetWithdrawList({
            ...args[0],
            search: this.search.search,
            timeFrom: this.search.timeFrom,
            timeTo: this.search.timeTo,
            status: this.search.status,
            type: this.search.type
          });
          if (result.code !== 0) {
            return this.$message.error(result.msg || '系统错误');
          }
          return result;
        });
        // 加载分页数据
        this.pagination.load();
      },

      // 审核申请
      setStatus({id}, status) {
        this.$confirm('此操作将' + (status === 2 ? '同意' : '拒绝') + '该提现申请, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          callback: async (confirm) => {
            if (confirm === 'confirm') {
              const result = await this.$service.applicationUpdateWithdrawStatus({id: id, status: status});
              if (result.code === 0) {
                this.$message.success(`操作成功~`);
                // 加载分页数据
                this.pagination.load();
              } else {
                this.$message.error(result.msg);
              }
            }
          }
        })
      },

      // 删除指定行
      deleteRow({id}) {
        this.$confirm('此操作将删除该提现申请, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          callback: async (confirm) => {
            if (confirm === 'confirm') {
              const result = await this.$service.applicationDeleteWithdrawApply({id: id});
              if (result.code === 0) {
                this.$message.success(`删除成功~`);
                // 加载分页数据
                this.pagination.load();
              } else {
                this.$message.error(result.msg);
              }
            }
          }
        });
      },

      // 导出
      handleDownload() {
        this.downloadLoading = true;
        const API_PATH = `${Config['API_TYPE'] === 'prod' ? Config['API_PATH'] : Config['API_PATH_DEV']}`;
        window.open(API_PATH + '/admin/transfer/export?token=' + this.$store.getters.token + '&page=' + this.pagination['data']['page'] +
          '&pageSize=' + this.pagination['data']['pageSize'] + '&search=' + this.search['search'] +
          '&status=' + this.search['status'] + '&timeFrom=' + this.search['timeFrom'] + '&timeTo=' + this.search['timeTo'] +
          '&mallId=' + this.$store.getters.sysUser.mallId);
        this.downloadLoading = false;
      }
    }
  }
</script>

<style lang="scss" type="text/scss" scoped>
  @import '../../styles/index.scss';
  .pagination {
    .header {
      @include flex-row(center, space-between);
      div{
        @include flex-row(center, flex-start);
        .search-input {
          width: 240px;
          margin-right: 5px;
        }
        .el-select{
          width: 160px;
          margin-right: 20px;
        }
      }
    }
  }
  .avatar{
    height: 72px;
    width: 72px;
  }
</style>
